/**
 * file管理初始化
 */
var file = {
		id: "fileTable",	//表格id
		seItem: null,		//选中的条目
		table: null,
		layerIndex: -1
};

/**
 * 初始化表格的列
 */
file.initColumn = function () {
	return [
	        {field: 'selectItem', radio: true},
	        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
	        {title: 'contentType', field: 'contentType', align: 'center', valign: 'middle', sortable: true},
	        {title: 'url', field: 'url', align: 'center', valign: 'middle', sortable: true,formatter: operateFormatter},
	        {title: 'size', field: 'size', align: 'center', valign: 'middle', sortable: true},
	        {title: 'path', field: 'path', align: 'center', valign: 'middle', sortable: true},
	        {title: '创建时间', field: 'createDate', align: 'center', valign: 'middle', sortable: true}

	        ];
};

/**
 * 处理登录状态
 */
function operateFormatter(value,row,index) 
//row 获取这行的值 ，index 获取索引值 
{ 
	
	var pro = window.location.protocol;
	var host = window.location.host;
	var domain = pro + "//" + host;
	var url = row['url'];
	var type = row['type'];
	var u = "";
	if(type == 1) {
		var src = domain + "/files" + url;
		u = "<a target='_blank' href='" + src +"'><img width='30' src='" + src + "'></img></a>";
	} else {
		u = url;
	}
	return u;
}



/**
 * 检查是否选中
 */
file.check = function () {
	var selected = $('#' + this.id).bootstrapTable('getSelections');
	if(selected.length == 0){
		EA.info("请先选中表格中的某一记录！");
		return false;
	}else{
		file.seItem = selected[0];
		return true;
	}
};




/**
 * 删除
 */
file.delete = function () {
	
	  if (this.check()) {

	        var operation = function(){
	            var ajax = new $ax("/file/delete", function () {
	                EA.success("删除成功!");
	                file.table.refresh();
	            }, function (data) {
	                EA.error("删除失败!" + data.responseJSON.message + "!");
	            });
	            ajax.set("id", file.seItem.id);
	            ajax.start();
	        };

	        EA.confirm("是否刪除该该文件，删除将不可恢复?", operation);
	    }
};


$(function () {
	var defaultColunms = file.initColumn();
	var table = new BSTable(file.id, "/file/list", defaultColunms);
	table.setPaginationType("server");
	file.table = table.init();
	
	
	 
	layui.use('upload', function(){
		var upload = layui.upload;
		upload.render({
		    elem: '#btn_upload' //绑定元素
		    ,accept: 'gif,jpg,jpeg,bmp,png' //允许上传的文件类型
		    ,url: '/file/upload' //上传接口
		    ,done: function(res, index, upload){
		    	EA.success("上传成功!");
                file.table.refresh();
		    }
		  });
	});
});
